<template>
    <el-card>
        <el-form>
            <el-row :gutter="12">
                <el-col :span="6">
                    <el-form-item label="姓名">
                        <el-input v-model="formData.name" placeholder="请输入姓名"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="手机号">
                        <el-input v-model="formData.phone" placeholder="请输入手机号"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="4">
                    <el-form-item label="sku角色">
                        <el-select v-model="formData.skuId">
                            <el-option v-for="item in skusList" :key="item.id" :label="item.title"
                                :value="item.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-button type="primary" @click="handleSearch">搜索</el-button>
                    <el-button @click="handleReset">重置</el-button>
                </el-col>
            </el-row>
        </el-form>
        <el-table :max-height="tableHeight - 32" :data="tableData" style="width: 100%">
            <el-table-column prop="name" label="姓名" width="200" align="center" />
            <el-table-column prop="phone" label="手机号" width="240" align="center" />
            <el-table-column label="sku角色">
                <template #default="{ row }">
                    <div class="tag">
                        <el-tag v-for="item in row.skus" :key="item.id">{{ item.title }}</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="createdAt" label="创建时间" width="200" align="center" />
            <el-table-column label="操作" width="200" align="center">
                <template #default="{ row }">
                    <el-button link type="primary" @click="handleUserInfo(row)">编辑</el-button>
                    <el-popconfirm title="确认删除？" @confirm="handleUserDelete(row)">
                        <template #reference>
                            <el-button link type="danger">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <Pagination :total="total" @pageChange="pageChange">
            <el-button type="primary" @click="handleUserInfo()">新增</el-button>
        </Pagination>
        <Dialog ref="dialogRef" :skusList="skusList" @submit="getTableData" />
    </el-card>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue"
import Pagination from '@/components/Pagination.vue'
import Dialog from "./components/Dialog.vue"
import { useGlobalStore } from '@/stores'
import { getUsers, getSku, deleteUsers } from "@/api";
import type { ResSku, ResUser } from "@/api/interface";
import { ElMessage } from 'element-plus'
const { tableHeight } = useGlobalStore()

const formData = reactive({
    name: "",
    phone: "",
    skuId: "",
    page: 1,
    size: 10
})

const tableData = ref<ResUser[]>([])
const total = ref<number>(0)
const getTableData = async () => {
    const { data } = await getUsers(formData)
    tableData.value = data.data
    total.value = data.total
}

const skusList = ref<ResSku[]>([])
const handleSkusList = async () => {
    const { data } = await getSku()
    skusList.value = data
}

const pageChange = (data: { page: number; size: number }) => {
    Object.assign(formData, data)
    getTableData()
}

const dialogRef = ref()
const handleUserInfo = (row?: ResSku) => {
    dialogRef.value.open(row)
}

const handleSearch = () => {
    formData.page = 1
    getTableData()
}

const handleUserDelete = async (data: ResSku) => {
    await deleteUsers(data.id)
    ElMessage.success('操作成功！')
    getTableData()
}

const handleReset = () => {
    Object.assign(formData, {
        name: "",
        phone: "",
        skuId: ""
    })
    getTableData()
}

getTableData()
handleSkusList()
</script>

<style scoped lang="scss">
.tag {
    display: flex;
    gap: 8px;
}
</style>